package com.cwnu.sw.cys.core.user.comsumer;

import com.cwnu.sw.cys.core.common.constant.MQConstant;
import com.cwnu.sw.cys.core.user.domain.dto.ScanSuccessMessageDTO;
import com.cwnu.sw.cys.core.websocket.service.WebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消费者类，用于处理扫描成功消息
 * 该类实现了RocketMQListener接口，用于监听和处理消息队列中的消息
 * 通过RocketMQMessageListener注解配置消费者组、主题和消息模式
 * @author jisam
 */
@RocketMQMessageListener(consumerGroup = MQConstant.SCAN_MSG_GROUP, topic = MQConstant.SCAN_MSG_TOPIC, messageModel = MessageModel.BROADCASTING)
@Component
@Slf4j
public class ScanSuccessConsumer implements RocketMQListener<ScanSuccessMessageDTO> {

    @Autowired
    private WebSocketService webSocketService;

    /**
     * 当接收到扫描成功消息时调用的方法
     * 该方法将调用WebSocketService的scanSuccess方法来处理扫描成功事件
     *
     * @param scanSuccessMessageDTO 扫描成功消息的DTO对象，包含扫描成功的相关信息
     */
    @Override
    public void onMessage(ScanSuccessMessageDTO scanSuccessMessageDTO) {
        webSocketService.scanSuccess(scanSuccessMessageDTO.getCode());
        log.info("扫描成功消息：{}", scanSuccessMessageDTO);
    }

}
